package netty.executor;

import io.netty.channel.DefaultEventLoopGroup;
import netty.util.StartAndStop;
import test.PerformanceAnalyze;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class EventLoopGroupZetas extends StartAndStop {

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    int size = 100;

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(size);

//    NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(100);
    DefaultEventLoopGroup nioEventLoopGroup = new DefaultEventLoopGroup(100);


    public void start() {
        for (int i = 0; i < size; i++) {
            scheduledExecutorService.scheduleAtFixedRate(() -> {
                long start = System.nanoTime();
                nioEventLoopGroup.execute(() -> {
                    performanceAnalyze.meter();
                    performanceAnalyze.delay(System.nanoTime() - start);
                });
            }, 1, 1, TimeUnit.MILLISECONDS);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new EventLoopGroupZetas().start();
        new CountDownLatch(1).await();
    }

}
